﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XbsWebAPI.EntityFrameworkCore;
using XbsWebAPI.EntityFrameworkCore.Entities;
using XbsWebAPI.EntityFrameworkCore.Entities.SystemAdmin;

namespace XbsWebAPI.Core.Repositories
{
    public class MenuRepository : IMenuRepository
    {
        private readonly DataContext _dbContext;

        public MenuRepository(DataContext dataContext)
        {
            _dbContext = dataContext;
        }


        public IQueryable<Menu> AsQueryable()
        {
           return _dbContext.Menus.AsQueryable();
           
        }
        //更新数据库
        public Menu Create(Menu entity)
        {
            entity.Id = Guid.NewGuid();
            entity.CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //entity.MenuType = (entity.PId == null || entity.PId == Guid.Empty)  ? MenuType.Parent : MenuType.Child;
            if(entity.MenuType == MenuType.Parent)
            {
                entity.ParentId = entity.Id;
            }else if(entity.MenuType == MenuType.Child)
            {
                entity.ParentId = entity.PId;
                entity.ChildId = entity.Id;
            }
            else
            {
                entity.ChildId = entity.PId;
                entity.ButtonId = entity.Id;
                var childMenu = _dbContext.Menus.FirstOrDefault(a => a.Id == entity.ChildId);
                if(childMenu != null)
                {
                    entity.ParentId = childMenu.PId;
                }
            }
            _dbContext.Add(entity);
            _dbContext.SaveChanges();

            return entity;
        }
        public Task<Menu> CreateAsync(Menu entity)
        {
            return Task.FromResult(Create(entity));
        }

        /// <summary>
        /// 弃用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Menu Delete(Guid id)
        {
            var model = _dbContext.Menus.FirstOrDefault(x => x.Id == id);
            if (model == null)
                return null;
            _dbContext.Remove(model);
            _dbContext.SaveChanges();

            return model;
        }

        public Task<Menu> DeleteAsync(string id)
        {
            throw new NotImplementedException();
        }

        public Task<Menu> DeleteAsync(Guid id)
        {
            var model = _dbContext.Menus.FirstOrDefault(x => x.Id == id);
            if (model == null)
                throw new Exception($"Id={id}的菜单不存在，无法进行删除操作");
            _dbContext.Remove(model);
            _dbContext.SaveChanges();

            return Task.FromResult(model);
        }

        public IQueryable<Menu> Query(Expression<Func<Menu, bool>> filter)
        {
            var query = _dbContext.Menus.Where(filter);

            return query;
        }

        /// <summary>
        /// 弃用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Menu QueryById(Guid id)
        {
            return _dbContext.Menus.FirstOrDefault(x => x.Id == id);
        }

        public Menu Update(Menu entity)
        {
            var model = _dbContext.Menus.AsNoTracking().FirstOrDefault(x => x.Id == entity.Id);
            if (model == null)
                return null;
            entity.CreationDate = model.CreationDate;
            entity.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            entity.Identifying = model.Identifying;
            //entity.MenuType = (entity.PId == null || entity.PId == Guid.Empty) ? MenuType.Parent : MenuType.Child;
            if (entity.MenuType == MenuType.Parent)
            {
                entity.ParentId = entity.Id;
            }
            else if (entity.MenuType == MenuType.Child)
            {
                entity.ParentId = entity.PId;
                entity.ChildId = entity.Id;
            }
            else
            {
                entity.ChildId = entity.PId;
                entity.ButtonId = entity.Id;
                var childMenu = _dbContext.Menus.FirstOrDefault(a => a.Id == entity.ChildId);
                if (childMenu != null)
                {
                    entity.ParentId = childMenu.PId;
                }
            }
            _dbContext.Menus.Update(entity);
            _dbContext.SaveChanges();

            return entity;
        }

        public Task<Menu> UpdateAsync(Menu entity)
        {
            return Task.FromResult(Update(entity));
        }


    }
}
